﻿using System;
using System.Web.UI;
using Coolite.Ext.Web;
using Pap.Data;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MainCommitteeDeposite : BasePage<MainCommitteeDepositeViewModel>, IPermittedPage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Ext.IsAjaxRequest && !Page.IsPostBack)
            {
                storeProjects.DataSource = this.ViewModel.GetUserProjects();
                storeProjects.DataBind();
                txtAssociationBalance.Text = this.ViewModel.GetMainBranchBalance().ToString();
                txtAssociationName.Text = this.ViewModel.GetMainBranchName();
            }
        }

        /// <summary>
        /// Handles the Clicked event of the BtnDeposit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnDeposit_Clicked(object sender, AjaxEventArgs e)
        {
            this.ViewModel.DepositInMainCommittee(cmbProjects.SelectedItem.Value.ToGuid(), double.Parse(txtDepositAmount.Text), Convert.ToDateTime(dateDepositDate.Value));
            GetProjectDeposites();
        }

        /// <summary>
        /// Handles the Changed event of the CmbProjects control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void CmbProjects_Changed(object sender, AjaxEventArgs e)
        {
            storeMainCommitteeDeposites.DataSource = this.ViewModel.GetMainBranchDeposites(cmbProjects.SelectedItem.Value.ToGuid());
            storeMainCommitteeDeposites.DataBind();
        }

        /// <summary>
        /// Gets the project deposites.
        /// </summary>
        private void GetProjectDeposites()
        {
            storeMainCommitteeDeposites.DataSource = this.ViewModel.GetMainBranchDeposites(cmbProjects.SelectedItem.Value.ToGuid());
            storeMainCommitteeDeposites.DataBind();
            txtDepositAmount.Reset();
            txtAssociationBalance.Text = this.ViewModel.GetMainBranchBalance().ToString();
            dateDepositDate.Reset();
        }

        public string PermissionName
        {
            get { return Permissions.MainCommitteeDeposite.ToString(); }
        }
    }
}
